<?php

require_once dirname(__FILE__).'/../lib/surveyGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/surveyGeneratorHelper.class.php';

/**
 * survey actions.
 *
 * @package    sf_sandbox
 * @subpackage survey
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class surveyActions extends autoSurveyActions
{
  public function executeSetstatus(sfWebRequest $request)
  {
    $this->forward404Unless($this->survey = Doctrine::getTable('Survey')->find(array($request->getParameter('id'))), sprintf('Object survey does not exist (%s).', array($request->getParameter('id'))));
    if(
      $request->getParameter("status")!="Under-construction" and
      $request->getParameter("status")!="On-going" and
      $request->getParameter("status")!="Closed"
      )
    {
      $this->getUser()->setFlash('error', 'Invalid status: '.$request->getParameter("status"));
    }
    $this->survey->setStatus($request->getParameter("status"));
    $this->survey->save();
    $this->redirect($request->getReferer());
  }
  public function executeShow(sfWebRequest $request)
  {
    $this->forward404Unless($this->survey = Doctrine::getTable('Survey')->find(array($request->getParameter('id'))), sprintf('Object survey does not exist (%s).', array($request->getParameter('id'))));
    $this->form = new SurveyForm($this->survey);
    $this->item = new Question();
    $this->item['survey_id']=$this->survey['id'];
    $this->itemform = new QuestionForm();
  }
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

    if(
      $this->survey->getStatus()=="On-going" or
      $this->survey->getStatus()=="Closed"
      )
    {
      $this->getUser()->setFlash('error', 'Ongoing or closed survey cannot be edited.');
      $this->redirect($request->getReferer());
    }
    elseif ($form->isValid())
    {
      $this->getUser()->setFlash('notice', $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.');

      $survey = $form->save();

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $survey)));

      if ($request->hasParameter('_save_and_add'))
      {
        $this->getUser()->setFlash('notice', $this->getUser()->getFlash('notice').' You can add another one below.');

        $this->redirect('@survey_new');
      }
      else
      {
        //custom redirect
        $this->redirect('@survey_show?id='.$survey['id']);
      }
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.');
    }
  }
}
